package com.example.tass.utils;

import java.nio.charset.Charset;
import java.util.Base64;
import java.util.regex.Pattern;

public class StringUtils {

    /**
     * Base64转成字符串
     *
     * @param base64String Base64格式字符串
     * @return String字符串
     */
    public static String Base64ToString(String base64String) {
        return new String(Base64.getDecoder().decode(base64String.getBytes()));
    }

    /**
     * String 字符串转换成Base64格式字符串
     *
     * @param inputString String字符串
     * @return Base64格式字符串
     */
    public static String stringToBase64String(String inputString) {
        return Base64.getEncoder().encodeToString(inputString.getBytes());
    }

    public static String stringToBase64String(String inputString,String charsetName) {
        return Base64.getEncoder().encodeToString(inputString.getBytes(Charset.forName(charsetName)));
    }

    public static boolean isBase64(String str) {
        String base64Rule = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        return Pattern.matches(base64Rule, str);
    }
}
